from selenium import webdriver
import time


def scrape_data_and_save_to_file():
    # 创建一个 Chrome WebDriver
    driver = webdriver.Chrome()

    url = 'http://127.0.0.1:9200/#/status/online'
    driver.get(url)
    api_address = None
    time.sleep(1)

    input = driver.find_element_by_name('email')
    input.send_keys('2350944438@qq.com')
    time.sleep(1)

    input1 = driver.find_element_by_name('password')
    input1.clear()
    time.sleep(1)
    input1.send_keys('loveshl2018')
    time.sleep(1)
    button = driver.find_element_by_class_name('el-button--primary')
    button.click()
    time.sleep(2)

    element = driver.find_element_by_xpath("//tr[2]//td[contains(@class, 'el-table_1_column_3')]/div")

    while api_address is None:
        api_address = element.text

    print(f"address={api_address}")

    with open("address.txt", "w", encoding="utf-8") as file:
        file.write(api_address)

    content = driver.page_source

    with open("webpage_source.html", "w", encoding="utf-8") as file:
        file.write(content)

    driver.quit()

    return api_address

# 调用函数来执行代码
# scrape_data_and_save_to_file()

